package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.host.dao.HostDetailDao;
import ysq.xyg.host.model.po.HostDetail;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostDetailVo;
import ysq.xyg.host.service.HostDetailService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 房产模块-佣金记录service实现
 *
 * @author shengQiang yu
 * @date 2019-7-5 22:09:26
 */
@Service("hostDetailService")
public class HostDetailServiceImpl extends BaseServiceImpl<HostDetail> implements HostDetailService {

    @Resource
    private HostDetailDao hostDetailDao;

    private HostDetail hostDetail;

    @Autowired
    public void setBaseDao(HostDetailDao hostDetailDao) {
        this.baseDao = hostDetailDao;
    }

    /**
     * 查询集合
     *
     * @param hostDetailVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:08:52
     */
    public List<HostDetail> listLeft(HostDetailVo hostDetailVo) {
        List<HostDetail> list = hostDetailDao.listLeft(hostDetailVo);
        if(list!=null&&list.size()>0){
            for (HostDetail hostDetail:list){
                if(StrUtil.isNotEmpty(hostDetail.getNickName())){
                    hostDetail.setNickName(BaseUtil.decodeBase64(hostDetail.getNickName()));
                }
            }
        }
        return list;
    }
    /**
     * 分页查询
     *
     * @param hostDetailVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:09:26
     */
    public ReturnEntity page(HostDetailVo hostDetailVo, int pageSize, int pageNum) {
        if(StrUtil.isNotEmpty(hostDetailVo.getNickName())){
            hostDetailVo.setNickName(BaseUtil.encodeBase64String(hostDetailVo.getNickName()));
        }
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostDetail> list = listLeft(hostDetailVo);
        PageInfo<HostDetail> page=new PageInfo<HostDetail>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostDetailVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:09:26
     */
    public ReturnEntity addOrUpdate(HostDetailVo hostDetailVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostDetailVo.getId())){
            hostDetail=new HostDetail(currentUser);
            isUpate=false;
        }else{
            hostDetail=selectByPrimaryKey(hostDetailVo.getId());
            if(ObjectUtil.isNull(hostDetail)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostDetail.setUpdateTime(new Date());
            hostDetail.setUpdateUser(currentUser.getId());
        }
        hostDetail.setMemberId(hostDetailVo.getMemberId());
        hostDetail.setReportId(hostDetailVo.getReportId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostDetail);
        }else{
            num=insert(hostDetail);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:09:26
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostDetail hostDetail=selectByPrimaryKey(id);
        if(hostDetail!=null){
            hostDetail.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostDetail.setUpdateTime(new Date());
            hostDetail.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostDetail);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostDetailVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-5 22:09:26
     */
    public List<HostDetail> acceptList(HostDetailVo hostDetailVo){
        Example example = new Example(HostDetail.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostDetailVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostDetailVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return hostDetailDao.selectByExample(example);
    }
}
